package cn.bdqn.dao.impl;

import cn.bdqn.dao.BaseDao;
import cn.bdqn.dao.PetDao;
import cn.bdqn.entity.Pet;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class PetDaoImpl extends BaseDao implements PetDao {
    @Override
    public List<Pet> queryPet() throws Exception {
        // 获取连接信息
        Connection connection = getConnection();
        //  查询SQL
        String querySql = "select id,`name`,type_name,health,love,birthday,owner_id, store_id from pet;";
        PreparedStatement pstmt = connection.prepareStatement(querySql);
        // 执行SQL
        ResultSet result = pstmt.executeQuery();
        // 创建宠物list 保存数据库里面的数据
        List<Pet> petList =  new ArrayList<Pet>();
        while(result.next()){
            // 创建一个空的宠物对象，保存数据
            Pet pet = new Pet();
            // 渲染数据   数据库的数据要与java实体类数据对齐
            pet.setId(result.getInt("id"));
            pet.setName(result.getString("name"));
            pet.setTypeName(result.getString("type_name"));
            pet.setHealth(result.getInt("health"));
            pet.setLove(result.getInt("love"));
            pet.setBirthday(result.getString("birthday"));
            pet.setOwnerId(result.getInt("owner_id"));
            pet.setStoreId(result.getInt("store_id"));

            // 讲pet对象存入List中
            petList.add(pet);
        }

        return petList;
    }
}
